Unlike
on-premise applications, the deployment of a cloud services involves
only software provisioning from the developer's perspective. You saw in
the earlier examples how hardware provisioning was abstracted from you
in the deployment process. In a scalable environment where enterprises
may need to provision multiple services across thousands of instances,
you need more programmatic control over the provision process rather
than configuring services using Windows Azure developer portal.
Manually uploading service packages and then starting and stopping
services from the portal interface works well for smaller services, but
becomes a time-consuming and error-prone process when deploying
multiple large-scale services. The Windows Azure Service Management API
allows you to programmatically perform most of the provisioning
functions via a REST-based interface to your Windows Azure cloud
account. Using the Service Management API, you can script your
provisioning and deprovisioning process end to end in an automated
manner. In this section, I will cover some important functions from the
Service Management API and also demonstrate some source code for you to
build your own cloud service provisioning process.
1. Service Management API Structure
The Service Management
API provides most of the functions you can perform on the storage
services and hosted services from Windows Azure developer portal. The
Service Management API categorizes the API operations into three
primary sections: storage accounts, hosted services, and affinity
groups. Operations on storage accounts mainly cover listing of accounts
and generation of the access keys. Operation on hosted services cover
listing of services, deploying services, removing services, swapping
between staging and production, and upgrading services. The affinity
groups operations are limited to listing and getting properties of
affinity groups in your account.
The Service Management API uses X.509 client certificates for authenticating calls between the client and the server.
The source code in the
following section is based on early CTP version (released October 10,
2009) of the service management API and its associated client assembly Microsoft.Samples.WindowsAzure.ServiceManagement. You can download the latest version of the assembly from the Windows Azure Code Samples page at http://code.msdn.microsoft.com/windowsazuresamples.
Even though the API may change in the future, the concepts used in this
section will remain the same past its final release. You may have
slightly modify the source code to make it work with the latest
available API.
|
|
2. Programming with the Service Management API
To start programming with the
Service Management API, you must first create a valid X.509 certificate
(or work with an existing one). You can use makecert.exe to create a
self-signed certificate
makecert -r -pe -a sha1 -n "CN=Windows Azure Authenticat
ion Certificate" -ss My -len 2048 -sp "Microsoft Enhanced RSA and AES
Cryptographic Provider" -sy 24 proazureservicemgmt.cer
Next, go to the Accounts
section of Windows Azure Developer portal and upload the certificate
from Manage API certificate section.
Once
the certificate is uploaded, you can call the Service Management REST
API by passing the certificate as the ClientCertificate property of the System.Net.HttpWebRequest object, by using the csmanage.exe
application from the Service Management API samples, or by building
your own application. In Ch3Solution, I have created a sample Windows
Application that makes REST calls to the Service Management API. It
uses the Microsoft.Samples.WindowsAzure.ServiceManagement.dll file from the service management code samples. The csmanage.exe uses the same assembly to make the API calls. Eventually, the API assembly may become part of the Windows Azure SDK. Figure 2 illustrates the Service Management API windows application.
In Figure 2,
The Service Management Operations section lists the operations that you
can invoke on the Service Management API. The output textbox prints the
output from the operations. The right-hand side of the user interface
consists of input parameters. The input parameters are as follows:
Subscription Id:
You can get the subscriptionId from the Account page of the developer
portal. This parameter is required by all the Service Management API
operations.
Certificate Path:
This text box points to the API certificate file on the local machine.
This certificate must match the one you uploaded to the portal.
Resource Type: This drop-down lists the types of resource you want to access: Hosted Service, Storage Account, or Affinity Group.
Resource name:
You should type the name of the resource you want to access (e.g.,
storage account name, hosted service name, affinity group name).
The remaining input
parameters are operation dependant. You can choose an operation from
the Service Management operations list, enter input parameters and
click Execute Operation. For example, to create a deployment in your
hosted service account, you can:
Select the Create Deployment operation.
Enter your Account SubscriptionId.
Select the API certificate from local machine.
Select Hosted Service Name as the Resource Type.
Enter the name of the Hosted Service you want to deploy your service to in the Resource Name text box.
Select the slot type (staging or production).
Choose a deployment name.
Choose a deployment label.
You have to then point to a service package (.cspkg) on a blob storage in the Package Blob URL text box.
Select the path to the ServiceConfiguration.cscfg file of the cloud service.
The OP-ID shows the
operation ID returned by the method call, which you can use to track
the operation status. To check the status of the deploy operation,
select the Get Operation Status method, and click Execute Operation.
The status gets displayed in the bottom window. Once the deployment is
complete, you can run the deployment by selecting the Update Deployment
Status method and selecting the "running" option from the deployment
status drop-down. Similarly, you can execute other operations from the
Service Management API.